package com.aha.dataanalyzeandview.service.impl;

import com.aha.dataanalyzeandview.config.MyThreadPoolExecutor;
import com.aha.dataanalyzeandview.entity.stock.StockBaseFinancialDataEntity;
import com.aha.dataanalyzeandview.entity.stock.StockDetailsInfoEntity;
import com.aha.dataanalyzeandview.mapper.ValueRecommendMapper;
import com.aha.dataanalyzeandview.service.IValueRecommendService;
import com.aha.dataanalyzeandview.utils.EasyPoiExcelUtils;
import com.aha.dataanalyzeandview.utils.FormatUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

/******************************
 * Project:           data-analyze-and-view
 * FileName:          ValueRecommendServiceImpl.java
 * ClassName:         ValueRecommendServiceImpl
 * Time:              2020/8/17 23:45
 * Version:           1.00
 * Author:            努力就是魅力
 * Description:       TODO
 ******************************/

@Slf4j
@Service
public class ValueRecommendServiceImpl implements IValueRecommendService {

    ThreadPoolExecutor pool = MyThreadPoolExecutor.buildThreadPool();

    @Autowired
    private ValueRecommendMapper valueRecommendMapper;

    @Override
    public List<StockDetailsInfoEntity> queryStockDetailsAndMainIndexByCondition(StockDetailsInfoEntity stockDetailsInfoEntity) {
        Map<String, Object> map = new HashMap<>();
        map.put("stockCode", stockDetailsInfoEntity.getStockCode());
        return valueRecommendMapper.queryStockDetailsAndMainIndexByCondition(map);
    }


    @Override
    public List<StockBaseFinancialDataEntity> queryStockBaseFinancialDataCondition(StockBaseFinancialDataEntity baseFinancialDataEntity) {
        return valueRecommendMapper.queryStockBaseFinancialDataCondition(baseFinancialDataEntity);
    }

    @Override
    public Integer importStockInfo(MultipartFile file) {

        AtomicInteger count = new AtomicInteger(0);
        List<StockDetailsInfoEntity> stockDetailsInfoEntityList = EasyPoiExcelUtils.importExcel(file, 0, 1, StockDetailsInfoEntity.class);
        for (StockDetailsInfoEntity s : stockDetailsInfoEntityList) {
            if (s == null) continue;
            pool.submit(() -> {
                int res = 0;
                if (s.getStockCode() != null) {
                    s.setStockCode(FormatUtils.formatStockCode(s.getStockCode()));
                }
                if (s.getPe() != null && (s.getPe() <= 2 || s.getPe() > 200 || s.getStockName().contains("ST"))) {
                    log.warn("剔除异常数据：" + s.toString());
                } else {
                    res = valueRecommendMapper.addStockMainInfo(s);
                }

                count.addAndGet(res);
                log.info("插入数据： " + s.toString());
            });

        }

//        int res = valueRecommendMapper.batchAddStockMainInfo(stockDetailsInfoEntityList);
//        log.info("导入股票数据条数：" + res);

        return count.get();

    }


    @Override
    public Integer importBasicFinancialData(MultipartFile file) {

        AtomicInteger count = new AtomicInteger(0);
        List<StockBaseFinancialDataEntity> stockBaseFinancialDataEntities = EasyPoiExcelUtils.importExcel(file, 0, 1, StockBaseFinancialDataEntity.class);
        for (StockBaseFinancialDataEntity s : stockBaseFinancialDataEntities) {
            if (s == null) {continue;}
            pool.submit(() -> {
            if (s.getStockName().contains("ST") || s.getStockName().contains("退") ||
                   s.getWeightedReturnOnEquity() == null || s.getWeightedReturnOnEquity()<0) {
                log.warn("剔除异常数据：" + s.toString());
            } else {
                // todo 格式化 股票代码
                if (s.getStockCode() != null) {
                    s.setStockCode(FormatUtils.formatStockCode(s.getStockCode()));

                    s.setWeightedReturnOnEquity(s.getWeightedReturnOnEquity() * 0.01F);
                    s.setSalesMargin(s.getSalesMargin() * 0.01F);
                    s.setBalanceSheetRatios(s.getBalanceSheetRatios() * 0.01F);
                int res = valueRecommendMapper.addBasicFinancialData(s);
                count.addAndGet(res);
                log.info("插入数据： " + s.toString());
                }
            }
            });
        }

//        int res = valueRecommendMapper.batchAddStockMainInfo(stockDetailsInfoEntityList);
//        log.info("导入股票数据条数：" + res);

        return count.get();
    }
}
